package com.aWei.springbootinit.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.aWei.springbootinit.common.BaseResponse;
import com.aWei.springbootinit.common.ErrorCode;
import com.aWei.springbootinit.common.ResultUtils;
import com.aWei.springbootinit.exception.ThrowUtils;
import com.aWei.springbootinit.model.dto.picture.PictureQuery;
import com.aWei.springbootinit.model.entity.Picture;
import com.aWei.springbootinit.service.PictureService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author 小维哥
 * @version 1.2         搜索图片的接口
 */
@RestController
@RequestMapping("/picture")
@Slf4j
public class PictureController {
        @Resource
        private PictureService pictureService;

        @PostMapping("/list/page/vo")
        public BaseResponse<Page<Picture>> listPostVOByPage(@RequestBody PictureQuery pictureQuery,
                                                            HttpServletRequest request) {

                long current = pictureQuery.getCurrent();
                long size = pictureQuery.getPageSize();//搜索个数
                if(pictureQuery.getSearchText()==null){//默认页面图片搜索内容
                        pictureQuery.setSearchText("洛天依");
                }
                // 限制爬虫
                ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
                String searchText = pictureQuery.getSearchText();
                Page<Picture> picturePage = pictureService.searchPicture(searchText,size ,current );
                return ResultUtils.success(picturePage);
        }
}
